<?php
namespace Manage\Controller;

use Manage\Controller\BaseController;
use Think\Controller;

class LetterController extends BaseController
{
    public $obj;
    public $temp_obj;
    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->obj = M('store_letter_type');
        $this->_arr['title'] = '短信类型设置';
    }

    public function index()
    {
        $this->_arr['title_two'] = '短信类型列表';
        $this->_arr['p'] = I('p', 1, 'intval');
        $where = '1=1';
        if($this->_arr['type_name']){
            $where .= " and type_name like '%".$this->_arr['type_name']."%''";
        }
        $sl_id = I('id') ? I('id') : 2;
        if ($sl_id == 1) {
            $order = 'slt_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($sl_id == 2) {
            $order = 'slt_id desc';
            $this->_arr['rank'] = 1;
        }
        $data = $this->obj
            ->where($where)
            ->order($order)
            ->page($this->_arr['p'] . ','.$this->page_num)
            ->select();
        //echo M()->_sql();
        $count = $this->obj->where($where)->count();// 查询满足要求的总记录数
        //dump($count);
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        //dump($show);
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();

    }

    /**
     * 新增
     */
    public function newLetter()
    {

        $this->_showDisplay();
    }

    public function addOrSaveLetter(){
        $id = I('id',0);
        //$data['slt_id'] = I('slt_id');
        $data['type_name'] = I('type_name') or $this->error('短信类型名称不能为空');
        $keywords = I('keywords') or $this->error('参数不能为空');
        $data['parameter'] = implode('#',$keywords);
        if($id){
            if(!is_numeric($id)){
                $this->error('参数错误');
            }
            $status = $this->obj->where(['slt_id' => $id])->save($data);
            if($status === false){
                $this->error('修改失败');
            }
        }else{
            $status = $this->obj->add($data);
            if($status === false){
                $this->error('新增失败');
            }
        }
        $this->success('成功', 'index');
    }

    /**
     * 修改
     */
    public function saveLetter()
    {
        $this->_arr['title_two'] = '短信类型编辑';
        $id = I('id') ? I('id') : $this->error('参数为空！');
        if (!is_numeric($id)) $this->error('无效参数！');
        $data = $this->obj->where(['slt_id' => $id])->find();
        $keywords = explode('#',$data['parameter']);
        $data['keywords'] = $keywords;
        $data['num'] = count($keywords);
        $this->_arr['info'] = $data;
        $this->_showDisplay('Letter/newLetter');
    }

    /**
     * 删除
     */
   /* public function deleteLetter(){
        $id = I('id') ? I('id') : exit(json_encode(['status' => false,'msg' => '参数为空']));
        if (!is_numeric($id)) {
            exit(json_encode(['status' => false,'msg' => '无效参数']));
        }
        $status = $this->obj->where(['sl_id' => $id])->delete();
        exit(json_encode($status === false ? ['status' => false,'msg' => '删除失败'] : ['status' => true,'msg' => '删除成功']));
    }*/

    public function __destruct()
    {
        unset($this->obj);
    }


}